<template>
    <el-card class="!border-none " shadow="never">
        
        
        <el-form class="-mb-4" :model="queryFormData" label-width="98px" inline>
            <div class="grid grid-cols-3 gap-1">
                <div>
                    <el-form-item label="入库单号" prop="warehouseOrderNo">
                        <el-input
                            placeholder="入库单号"
                            
                            class="w-64"
                            v-model="queryFormData.warehouseOrderNo"
                            clearable
                            @keyup.enter="resetOnePage"
                        >
                        
                        </el-input>
                    </el-form-item>
                </div>
    
    
                <div>
                    <el-form-item label="物流单号" prop="warehouseType">
                        <el-input
                            placeholder="仓库类型"
                
                            class="w-64"
                            v-model="queryFormData.warehouseType"
                            clearable
                            @keyup.enter="resetOnePage"
                        >
            
                        </el-input>
                    </el-form-item>
                </div>
    
        
                
                <div class="flex flex-row-reverse">
                    <el-form-item>
                        <div class="flex-1  flex flex-row-reverse  w-full ">
                            <el-button class="mx-2" type="primary" @click="getPageLists">查询</el-button>
                            <el-button class="mx-2" @click="resetParams">重置</el-button>
                        
                        </div>
                    </el-form-item>
                </div>
            
            </div>
        </el-form>
    
    
    </el-card>
    
    
    <el-card class="!border-none mt-5" shadow="never">
        
        <div class="flex">
            <div class="flex-1 mb-2">
                <el-button type="primary" @click="handleCreateWarehousingOrder()">
                    <template #icon>
                        <icon name="el-icon-Plus"/>
                    </template>
                    提交入库单
                </el-button>
            </div>
    
    
            <div class=" flex mt-2 justify-end">
                <el-tag
                    class="mx-2 hover:text-danger hover:bg-warning   cursor-pointer"
                    @click="getCurrentStatus(null)"
                    effect="light"
                    round
                    type="info">
                    全部
                </el-tag>
        
                <el-tag v-for="(item,index) in afterSaleOrderConfig.warehouseOrderStatus"
                        class="mx-2 hover:text-danger hover:bg-warning   cursor-pointer"
                        @click="getCurrentStatus(item.code)"
                        effect="light"
                        :key="index"
                        round
                        type="info">
                    {{ item.name }}
                </el-tag>
            </div>


        </div>
  
        
 
        <el-table
            ref="tableRef"
            size="large"
            v-loading="pagerData.loading"
            :data="pagerData.lists"
            row-key="id"
        >
            <el-table-column label="入库单号" prop="warehouseOrderNo" min-width="170"/>
            
            <el-table-column label="状态" prop="status" min-width="120">
                <template #default="{ row }">
                    
                    <el-tag v-for="(item,index) in afterSaleOrderConfig.warehouseOrderStatus"
                            v-show="row.status === item.code"
                            :key="index"
                            :disable-transitions="true"
                            :type="item.type">
                        {{ item.name }}
                    </el-tag>
                </template>
            </el-table-column>
            
            
            <el-table-column label="创建时间" prop="createTime " min-width="180"
                             :formatter="formatterManager.formatterCreateTime"/>
    
    
            <el-table-column label="更新时间" prop="updateTime " min-width="180"
                             :formatter="formatterManager.formatterUpdateTime"/>
    
    
            <el-table-column label="操作人" prop="optPerson" min-width="150"/>
            
            <el-table-column label="操作" width="170" fixed="right">
                <template #default="{ row }">
                    <el-button
                        type="primary"
                        link
                        @click="handleRowDetail(row)"
                    >
                        详情
                    </el-button>
                    
                    
                    <el-button
                        v-if="row.status===AfterSaleWarehouseStatusEnum.FAIL"
                        type="primary"
                        link
                        @click="handleAgain(row)"
                    >
                        重试
                    </el-button>
                
                
                </template>
            </el-table-column>
        </el-table>
        
        <pagination v-model="pagerData" @change="getPageLists"/>
    
    </el-card>
    
    
    <Detail ref="detailRef" @close="showDetailRef=false" v-if="showDetailRef"/>
    <Edit ref="editRef" @close="showEditRef=false" v-if="showEditRef"/>

</template>

<script lang='ts' setup>
import Edit from './edit.vue'
import {usePagerManager} from "@/manager/base/PagerManager";
import feedback from "@/utils/feedback";
import Detail from '@/components/order-detail/detail.vue'
import {closeAfterSaleOrder} from "@/api/afterSale/order/AfterSaleOrderController";
import {getWarehousingList} from "@/api/afterSale/order/AfterSaleOrderListController";
import {UnwrapNestedRefs} from "@vue/reactivity";

import {AfterSaleOrderDetailDTO} from "@/api/afterSale/order/dto/AfterSaleOrderDetailDTO";
import {useFormatterManager} from "@/manager/base/FormatterManager";
import {useAfterSaleEnumManager} from "@/manager/base/AfterSaleEnumManager";
import AfterSaleWarehouseSearchRequest from "@/api/afterSale/warehouse/request/AfterSaleWarehouseSearchRequest";
import {optWarehouseOrderWarehousing} from "@/api/afterSale/warehouse/AfterSaleWarehouseController";
import IdRequest from "@/api/baisc/IdRequest";
import {AfterSaleWarehouseStatusEnum} from "@/config/enums/aftersale/AfterSaleWarehouseEnum";


let afterSaleOrderConfig = useAfterSaleEnumManager();
let formatterManager = useFormatterManager(afterSaleOrderConfig);

const editRef = shallowRef(Edit)
const showEditRef = ref(false)
const detailRef = shallowRef(Detail)
const showDetailRef = ref(false)

// 查询请求数据
let queryFormData: UnwrapNestedRefs<AfterSaleWarehouseSearchRequest> = reactive(new AfterSaleWarehouseSearchRequest)

// 分页数据
const {pagerData, getPageLists, resetParams, resetOnePage} = usePagerManager({
    fetchFun: getWarehousingList,
    params: queryFormData
})


/**
 * 创建入库工单
 *
 * @author 徐寿春
 * 2023/4/6 13:44
 */
async function handleCreateWarehousingOrder() {
    showEditRef.value = true
    await nextTick()
    editRef.value?.open()
}



/**
 *  重试当前入库调用
 *
 * @author 徐寿春
 * 2023/4/6 13:44
 */
async function handleAgain(row: any) {
    let idRequest = new IdRequest();
    idRequest.id = row.warehouseOrderId
    optWarehouseOrderWarehousing(idRequest).then(() => {
        getPageLists()
        feedback.notifySuccess("重试入库调用成功")
    })
}

/**
 *  删除工单
 *
 * @author 徐寿春
 * 2023/4/6 13:44
 */
async function handleDelete(row: any) {
    await feedback.confirm("确定关闭当前工单？")
    await closeAfterSaleOrder({id: row.afterSaleOrderId})
    await getPageLists()
}


/**
 *  获取当前状态 -- 点击
 *
 * @author 徐寿春
 * 2023/4/6 13:44
 */
function getCurrentStatus(row: any) {
    getPageLists()
}


// ------------------------------------------ ** 处理详情 ** ------------------------------------------------------------------------------------


/**
 *  处理详情页
 *
 * @author 徐寿春
 * 2023/4/6 13:45
 */
async function handleRowDetail(row: AfterSaleOrderDetailDTO) {
    showDetailRef.value = true
    await nextTick()
    detailRef.value?.open(row.afterSaleOrderId)
}


/**
 * 获取工单列表
 */
onMounted(() => {
    getPageLists()
    
})


</script>

